package com.originskyseed.platform.role.provider;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.role.mapper.RoleRelMenuMapper;
import com.originskyseed.platform.role.model.RoleRelMenu;
import com.originskyseed.platform.role.service.IRoleRelMenuService;

/**
 * 角色与菜单关系表Service接口实现类（OSI_ROLE_REL_MENU）
 * 
 * @author Tangwanghui
 */
@DubboService
@Component("roleRelMenuService")
public class RoleRelMenuServiceImpl implements IRoleRelMenuService {

	@Resource
	private RoleRelMenuMapper roleRelMenuMapper;

	/**
	 * 删除记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.roleRelMenuMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 * 
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.roleRelMenuMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(RoleRelMenu record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.roleRelMenuMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(RoleRelMenu record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.roleRelMenuMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(RoleRelMenu record) throws Exception {
		record.setUpdateTime(new Date());
		return this.roleRelMenuMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(RoleRelMenu record) throws Exception {
		record.setUpdateTime(new Date());
		return this.roleRelMenuMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public RoleRelMenu selectByPrimaryKey(String id) throws Exception {
		return this.roleRelMenuMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<RoleRelMenu> selectList(RoleRelMenu record) throws Exception {
		return this.roleRelMenuMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 * 
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<RoleRelMenu> selectListByPage(Map<String, Object> params, BasePage<RoleRelMenu> page) throws Exception {
		params.put("page", page);
		List<RoleRelMenu> list = this.roleRelMenuMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 根据角色标识重新关联记录
	 *
	 * @param roleId
	 * @param permissionIds
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteAndInsertByRoleId(String roleId, List<String> permissionIds) throws Exception {
		// 根据角色标识删除关联记录
		int result = this.roleRelMenuMapper.deleteByRoleId(roleId);

		// 重新插入角色与权限关联记录
		for (String permissionId : permissionIds) {
			RoleRelMenu roleRelPermission = new RoleRelMenu();
			roleRelPermission.setRoleId(roleId);
			roleRelPermission.setPermissionId(permissionId);
			result += this.insert(roleRelPermission);
		}
		return result;
	}

	/**
	 * 根据权限标识重新关联记录
	 *
	 * @param permissionId
	 * @param roleIds
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteAndInsertByPermissionId(String permissionId, List<String> roleIds) throws Exception {
		// 根据权限标识删除关联记录
		int result = this.roleRelMenuMapper.deleteByPermissionId(permissionId);

		// 重新插入角色与权限关联记录
		for (String roleId : roleIds) {
			RoleRelMenu roleRelPermission = new RoleRelMenu();
			roleRelPermission.setRoleId(roleId);
			roleRelPermission.setPermissionId(permissionId);
			result += this.insert(roleRelPermission);
		}
		return result;
	}

	/**
	 * 根据角色标识获取关联记录
	 *
	 * @param roleId
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<RoleRelMenu> selectByRoleId(String roleId) throws Exception {
		return this.roleRelMenuMapper.selectByRoleId(roleId);
	}

	/**
	 * 根据权限标识获取关联记录
	 *
	 * @param permissionId
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<RoleRelMenu> selectByPermissionId(String permissionId) throws Exception {
		return this.roleRelMenuMapper.selectByPermissionId(permissionId);
	}

	/**
	 * 是否存在角色关联记录
	 *
	 * @param permissionIds
	 * @return
	 * @throws Exception
	 */
	@Override
	public Boolean existRoleId(List<String> permissionIds) throws Exception {
		return this.roleRelMenuMapper.existRoleId(permissionIds);
	}

	/**
	 * 是否存在权限关联记录
	 *
	 * @param roleIds
	 * @return
	 * @throws Exception
	 */
	@Override
	public Boolean existPermissionId(List<String> roleIds) throws Exception {
		return this.roleRelMenuMapper.existPermissionId(roleIds);
	}

}